package com.cloudbroker.bcs.platform.dav.middle.bizservice.impl;

import static com.alibaba.fastjson.JSON.parseObject;
import static com.alibaba.fastjson.JSON.toJavaObject;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.cloudbroker.bcs.common.constants.BasicFields;
import com.cloudbroker.bcs.common.util.CollectionUtil;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.QQUrlAndConstant;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes.ClientErr;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.QQBizService;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.request.GetQQOpenIDForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.request.GetQQUserInfoForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.request.GetWeiboAccessTokenForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.response.GetQQUserInfoBackVo;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.response.QQUserInfoBackVo;
import com.cloudbroker.bcs.platform.dav.common.config.ConfigValues;
import com.cloudbroker.bcs.platform.dav.common.constant.IFSField;
import com.cloudbroker.bcs.platform.dav.common.util.HttpUtil;
@Service
public class QQBizServiceImpl implements QQBizService {

	@Override
	public GetQQUserInfoBackVo getUserinfo(GetQQUserInfoForm form) {
		try {
			JSONObject json = parseObject(HttpUtil.doGet(QQUrlAndConstant.GET_QQ_USER_INFO, form));
			CollectionUtil.transKey(json , IFSField.ERRCODE , BasicFields.EXT_ERROR_NO);
	        CollectionUtil.transKey(json , IFSField.ERRMSG , BasicFields.EXT_ERROR_INFO);
	        QQUserInfoBackVo userInfo = toJavaObject(json, QQUserInfoBackVo.class);
			if(userInfo.getRet() == null || !"0".equals(userInfo.getRet())){
				return VOUtil.genErrorResult(ClientErr.WX_USERINFO_FAIL,GetQQUserInfoBackVo.class);
			}
			GetQQUserInfoBackVo backData = new GetQQUserInfoBackVo();
			backData.setOpenid(form.getOpenid());
			backData.setNickname(userInfo.getNickname());
			backData.setAvatar(userInfo.getFigureurl_qq_1());
			return VOUtil.setSuccessResult(backData);
		} catch (Exception e) {
			return VOUtil.genErrorResult(ClientErr.WX_USERINFO_FAIL,GetQQUserInfoBackVo.class);
		}
	}

	@Override
	public GetQQUserInfoBackVo getQQOpenID(GetQQOpenIDForm form) {
		try {
			String jsonStr = HttpUtil.doGet(QQUrlAndConstant.GET_QQ_OPENID, form);
			if(jsonStr.indexOf("{") != -1){
                jsonStr = jsonStr.substring(jsonStr.indexOf("{"), jsonStr.indexOf("}")+1);
            }
		    JSONObject json = parseObject(jsonStr);
			GetQQUserInfoForm param = toJavaObject(json, GetQQUserInfoForm.class);
			if(param.getOpenid() == null || "".equals(param.getOpenid())){
				return VOUtil.genErrorResult(ClientErr.WX_USERINFO_FAIL,GetQQUserInfoBackVo.class);
			}
			param.setOauth_consumer_key(ConfigValues.getQQAppKey());
			param.setAccess_token(form.getAccess_token());
			return getUserinfo(param);
		} catch (Exception e) {
			return VOUtil.genErrorResult(ClientErr.WX_USERINFO_FAIL,GetQQUserInfoBackVo.class);
		}
	}

	@Override
	public GetQQUserInfoBackVo getUserinfoByCode(
			GetWeiboAccessTokenForm form) {
		form.setClient_id(ConfigValues.getQQAppKey());
		form.setClient_secret(ConfigValues.getQQAppSecret());
		form.setGrant_type(QQUrlAndConstant.AUTHORIZATION_CODE);
		form.setRedirect_uri(ConfigValues.getQQRedirectUri());
		try {
			String jsonStr =HttpUtil.doGet(QQUrlAndConstant.GET_QQ_ACCESS_TOKEN, form);
			GetQQOpenIDForm param = new GetQQOpenIDForm();
			if(jsonStr.indexOf("{") != -1){
			    jsonStr = jsonStr.substring(jsonStr.indexOf("{"), jsonStr.indexOf("}")+1);
			    JSONObject json = parseObject(jsonStr);
	            param = toJavaObject(json, GetQQOpenIDForm.class);
			}else{
			    String[] backArr1 = jsonStr.split("&");
			    for(String str1 : backArr1){
			        String[] backArr2 = str1.split("=");
			        if("access_token".equals(backArr2[0])){
			            param.setAccess_token(backArr2[1]);
			        }
			    }
			}
			if(param.getAccess_token() == null || "".equals(param.getAccess_token())){
				return VOUtil.genErrorResult(ClientErr.WX_USERINFO_FAIL,GetQQUserInfoBackVo.class);
			}
			return getQQOpenID(param);
		} catch (Exception e) {
			return VOUtil.genErrorResult(ClientErr.WX_USERINFO_FAIL,GetQQUserInfoBackVo.class);
		}
	}

}
